package com.superzqb.asus.superzqbFragment.superzqbShopMain.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Bean.GoodsDetailBean;
import com.superzqb.asus.superzqbUtil.Url;

import java.util.List;

/**
 * Created by asus on 2017/3/11.
 */

public class GoodsDetailAdapter extends BaseAdapter {
    Context context;
    List<GoodsDetailBean> list;
    private int select =-1;
    public GoodsDetailAdapter(Context context, List<GoodsDetailBean> list){
        this.context=context;
        this.list=list;
    }
    public void setList(List<GoodsDetailBean> list) {
        this.list = list;
    }
    @Override
    public int getCount() {
        return list == null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.goods_detail_item,parent,false);
            convertView.setTag(new GoodsDetailAdapter.ViewHolder(convertView));
        }
        GoodsDetailAdapter.ViewHolder holder=(GoodsDetailAdapter.ViewHolder) convertView.getTag();
        GoodsDetailBean goods=(GoodsDetailBean)getItem(position);
        holder.gName.setText(goods.getName());
        //String.format("%.2f", f)
        Double d = Double.parseDouble(goods.getPrice());
        holder.time.setText(String.format("%.2f", d/12)+"(元/天)");
        holder.price.setText(goods.getPrice());
        holder.sales.setText(goods.getSales()+"");
        Picasso.with(context).load(new Url().getImg_url() +goods.getImg()).into(holder.gImg);
        if(position==select){
            holder.gName.setTextColor(context.getResources().getColor(R.color.colorFont));
        }else{
            holder.gName.setTextColor(context.getResources().getColor(R.color.black));
        }
        return convertView;
    }
    public void changeColor(int position) {
        this.select=position;
    }
    class ViewHolder{
        private ImageView gImg;
        private TextView gName,time,price,sales;
        private LinearLayout all;
        public ViewHolder(View view){
            gImg=(ImageView)view.findViewById(R.id.img);
            gName=(TextView)view.findViewById(R.id.gname);
            time=(TextView)view.findViewById(R.id.time);
            price=(TextView)view.findViewById(R.id.price);
            sales=(TextView)view.findViewById(R.id.sales);
            all=(LinearLayout)view.findViewById(R.id.all);
        }

    }
}
